package com.banzhi.shop.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.data.GroupProfitBean;
import com.banzhi.shop.data.GroupProfitBean.DataBean;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.banzhi.shop.MyApp.context;

/**
 * Created by Administrator on 2018/7/26.
 */

public class GroupGoodsDetailAdapter extends RecyclerView.Adapter<GroupGoodsDetailAdapter.ViewHolder>{
    private List<GroupProfitBean.DataBean> mList;
    private GroupGoodsDetailAdapter.OnItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        View myView;

        TextView title;
        TextView createTime;
        TextView profit;


        public ViewHolder(View itemView) {
            super(itemView);
            myView = itemView;

            title = (TextView) itemView.findViewById(R.id.tv_title);
           createTime = (TextView) itemView.findViewById(R.id.tv_create_time);
            profit = (TextView) itemView.findViewById(R.id.tv_profit);

        }
    }

    public GroupGoodsDetailAdapter(List<GroupProfitBean.DataBean> list){
        this.mList = list;
    }

    @Override
    public GroupGoodsDetailAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recylerview_item_goods_detail,null);
        final GroupGoodsDetailAdapter.ViewHolder holder = new GroupGoodsDetailAdapter.ViewHolder(view);
   /*     //条目点击控件
        holder.myView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                GroupProfitBean.DataBean groupsBean = mList.get(position);
                if (mOnItemClickListener != null) {
                    //注意这里使用getTag方法获取position
                    mOnItemClickListener.onItemClick(view,(int)view.getTag());
                }

            }
        });
 */

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        GroupProfitBean.DataBean bean = mList.get(position);
        holder.title.setText(bean.getTitle());
        holder. profit.setText(bean.getGroupsprice());
        holder.createTime.setText(bean.getCreatetime());

        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }


    //下面两个方法提供给页面刷新和加载时调用
    public void add(List<GroupProfitBean.DataBean> addMessageList) {
        //增加数据
        int position = mList.size();
        mList.addAll(position, addMessageList);
        notifyItemInserted(position);
    }

    public void refresh(List<GroupProfitBean.DataBean> newList) {
        //刷新数据
        mList.removeAll(mList);
        mList.addAll(newList);
        notifyDataSetChanged();
    }


    public void setOnItemClickListener(GroupGoodsDetailAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }




}
